package com.tpshion.cloud.core.config;

import com.tpshion.cloud.common.support.Field;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Configuration
public class FeignConfig implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        if(!StringUtils.isEmpty(request.getHeader(Field.TOKEN))){
            //添加token
            requestTemplate.header(Field.TOKEN, request.getHeader(Field.TOKEN));
        }
    }
}
